package com.friend.campusnotice;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.friend.campusnotice.adapter.ContactListAdapter;
import com.friend.campusnotice.bean.Contacts;
import com.friend.campusnotice.util.MyUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/5/25.
 */
public class ContactsFragment extends Fragment implements View.OnFocusChangeListener{

    private RecyclerView recyleView;
    private ContactListAdapter adapter;
    private List<Contacts> contactsList;
    private List<Contacts> teampContactsList = new ArrayList<Contacts>();
    private Map<String, Object> myData;
    private EditText searchContacts;
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter.notifyDataSetChanged();
            // Toast.makeText(MainActivity.this,"加载完了",Toast.LENGTH_LONG).show();

        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.contacts_layout,container,false);
        initView(view);
        initData();
        return view;

    }

    private void initView(View view) {
        recyleView = (RecyclerView) view.findViewById(R.id.swipe_target);
        searchContacts = (EditText) view.findViewById(R.id.search_contacts);
        searchContacts.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                contactsList.clear();;
                contactsList.addAll(teampContactsList);
                HashSet<String> set = new HashSet<String>();
                Log.i("xiao","获取到的文本 "+ s);

                Log.i("xiao","contactsList 的原始长度 "+ contactsList.size());
                List<Contacts> sub = new ArrayList<Contacts>();
                for (int i =0;i<contactsList.size();i++){
                    Pattern p = Pattern.compile(s+"");
                    Matcher namem = p.matcher(contactsList.get(i).getContactName());
                    Matcher numberm = p.matcher(contactsList.get(i).getPhoneNumber());
                    while (namem.find()) {
                        Log.i("xiao","匹配成功 "+ s +" "+ contactsList.get(i).getContactName());
                        if(set.add(contactsList.get(i).getContactName())){
                            sub.add(contactsList.get(i));
                        }

                        break;
                    }
                    while (numberm.find()) {
                        Log.i("xiao","匹配成功 "+ s +" "+ contactsList.get(i).getPhoneNumber());
                        if(set.add(contactsList.get(i).getContactName())){
                            sub.add(contactsList.get(i));
                        }
                        break;
                    }
                }
                Log.i("xiao","sub 的长度 "+sub.size());
                contactsList.clear();
                contactsList.addAll(sub);
                Log.i("xiao","contactsList 的长度 "+contactsList.size());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        contactsList = new ArrayList<Contacts>();
        adapter = new ContactListAdapter(getActivity(), contactsList);
        adapter.setOnItemClickListener(new ContactListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getActivity(),"获取的位置"+ position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(),ContactDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("contact", contactsList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyleView.setLayoutManager(layoutManager);
        recyleView.setAdapter(adapter);

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            Toast.makeText(getActivity(),"获取了返回值",Toast.LENGTH_LONG).show();
        }
    }

    private void initData() {

        new Thread(){
            @Override
            public void run() {
                contactsList.clear();
                MyUtil myUtil = new MyUtil();
                myData = myUtil.getPhoneContacts(getActivity());
                ArrayList<String> name = (ArrayList<String>) myData.get("ContactsName");
                ArrayList<String> number = (ArrayList<String>) myData.get("ContactsNumber");
                ArrayList<Long> ContactId = (ArrayList<Long>) myData.get("ContactId");
                ArrayList<Long> ContactIconId = (ArrayList<Long>) myData.get("ContactsIconId");
                for (int i = 0; i < name.size(); i++) {
                    Contacts contact = new Contacts();
                    contact.setContactName(name.get(i));
                    contact.setPhoneNumber(number.get(i));
                    contact.setContact_Id(ContactId.get(i));
                    contact.setPhotoid(ContactIconId.get(i));
                    //contact.setUserHead(phonto.get(i));
                    contactsList.add(contact);
                    teampContactsList.clear();
                    teampContactsList.addAll(contactsList);
                    handler.sendEmptyMessage(0);
                }
            }
        }.start();




    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()){
            case R.id.search_contacts:
                if (hasFocus){
                    Toast.makeText(getActivity(),"获得焦点",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getActivity(),"失去焦点",Toast.LENGTH_SHORT).show();
                }
        }
    }
}
